package com.easyexcle.demo.exportEasy;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.easyexcle.demo.easyExcel.style.ExcelWidthStyleStrategy;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: lkz
 * @Title: DynamicTableTest
 * @Description: 动态表头
 * @Date: 2023/4/12 16:29
 */

public class DynamicTableTest {


    public static void main(String[] args) {
        String fileName = "d:\\文件名称" + System.currentTimeMillis() + ".xlsx";
        EasyExcel.write(fileName)
                //.needHead(false) //不设置表头
                // 这里放入动态头
                .head(head()).sheet("模板").useDefaultStyle(false).registerWriteHandler(setConfigure())
                .registerWriteHandler(new ExcelWidthStyleStrategy())
                // 当然这里数据也可以用 List<List<String>> 去传入
                .doWrite(dataList());

    }


    private static List<List<String>> head() {
        List<List<String>> list = new ArrayList<>();
        List<String> head0 = new ArrayList<>();
        String empty=" ";
        head0.add("访视表");
        head0.add("eCRF/Visit");
        List<String> head1 = new ArrayList<>();
        head1.add(empty);
        head1.add("筛选期");
        List<String> head2 = new ArrayList<>();
        head2.add(empty);
        head2.add("术后辅助_CnD1");
        List<String> head3 = new ArrayList<>();
        head3.add(empty);
        head3.add("疾病进展后治疗_CnD1");
        List<String> head4 = new ArrayList<>();
        head4.add(empty);
        head4.add("肿瘤评估");
        List<String> head5 = new ArrayList<>();
        head5.add(empty);
        head5.add("日志");
        List<String> head6 = new ArrayList<>();
        head6.add(empty);
        head6.add("计划外访视");

        list.add(head0);
        list.add(head1);
        list.add(head2);
        list.add(head3);
        list.add(head4);
        list.add(head5);
        list.add(head6);
        return list;
    }

    private static List<List<Object>> dataList() {
        List<List<Object>> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            List<Object> data = new ArrayList<>();
            data.add("字符串" + i);
            data.add("0.56-------------\n 是 测试数据√ 研究数据√ \n 否 测试名字 √");
            data.add(new Date());
            list.add(data);
        }
        return list;
    }

    //配置字体，表头背景等
    private static HorizontalCellStyleStrategy setConfigure() {
        // 头的策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        // 背景色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        WriteFont headWriteFont = new WriteFont();
        // 加粗
        headWriteFont.setBold(false);
//        headWriteFont.setFontHeightInPoints((short) 14);
        headWriteCellStyle.setWriteFont(headWriteFont);


        // 内容的策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        // 字体策略
        WriteFont contentWriteFont = new WriteFont();
        // 字体大小
//        contentWriteFont.setFontHeightInPoints((short) 14);
        contentWriteCellStyle.setWriteFont(contentWriteFont);
        //边框
        //导出数据垂直居中
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        //导出数据水平居中
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        contentWriteCellStyle.setBorderLeft(BorderStyle.NONE);
        contentWriteCellStyle.setBorderTop(BorderStyle.NONE);
        contentWriteCellStyle.setBorderRight(BorderStyle.NONE);
        contentWriteCellStyle.setBorderBottom(BorderStyle.NONE);

        //设置 自动换行
        contentWriteCellStyle.setWrapped(true);
        //设置
        // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
        return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
    }


}
